package com.goodfox.web.generic.provide.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer;
import com.thoughtworks.xstream.io.xml.XppDriver;

/**
 * XStream工厂类，用于取得XStream实例等
 * XStream是XML序列化与反序列化工具，配合annotation可以实现JavaBean与XML的数据绑定
 * 
 * @author sunyu
 * @version 2011-06-07
 * 
 */
public class XStreamFactory {

	/**
	 * 获取XStream的实例，用于将XML反序列化为对象
	 * 
	 * @return XStream的实例
	 */
	public static XStream getFromXMLInstance() {

		// 默认的DomDriver采用xpp3
		return new XStream();

	}

	/**
	 * 创建一个将对象转化为XML的XStream实例，解决了XStream生成XML的"__"Bug
	 * 
	 * @return 用于将对象转换为XML的XStream实例
	 * 
	 */
	public static XStream getToXMLInstance() {
		/**
		 * XmlFriendlyReplacer 用于解决XStream将对象中field名称中的"_"转化成XML中的"__"的BUG
		 */
		XStream xStream = new XStream(new XppDriver(new XmlFriendlyReplacer(
				"__", "_")));
		return xStream;
	}
}
